/*********************************************************************************
Class Name: NMRHotLeadcontroller
Author: Accenture
Date:2/20/2012 
Requirement/Project Name: NMR  
Requirement Description:  a view in home page of salesforce for all HOT leads
Version History:
**********************************************************************************/
Public Class NMRHotLeadcontroller{

    Public List<Lead> leadList{get;set;}//to hold leads for current page
    Public Id recordTypeIdLead{get;set;}//To hold NMR Recordtype
    Public Id currentUserId;//To hold current user
    public string selectedLeadId { get; set; }
    
    public static Id nmrLeadCustIdRecordTypeID = RecordTypeHelper.GetRecordTypeID('Lead', 'NMR Lead with CustomerId');
    public NMRHotLeadcontroller(ApexPages.StandardSetController controller) {
        getLoadData();
    }
    //Standard Set Controller to hold the hot leads
    public ApexPages.StandardSetController leadCon{
        get {
            if(leadCon == null) {
                currentUserId = UserInfo.getUserId();
                recordTypeIdLead = RecordTypeHelper.GetRecordTypeID('Lead', 'NMR Lead');
                leadCon = new ApexPages.StandardSetController(Database.getQueryLocator([select id,name,lastname,Priority__c,firstname,Leadsource,Rating,company,phone,status,Hot_Lead__c from lead where (ownerId =: CurrentUserId OR NMR_Sales_Rep__c =: CurrentUserId) AND ((Priority__c =: '01 - Critical' AND Status =: 'NEW') OR Hot_Lead__c = TRUE) AND (recordtypeId =: RecordtypeIdlead OR recordtypeId =: nmrLeadCustIdRecordTypeID) AND Status!='Closed' ORDER BY Hot_Lead_Sorter__c LIMIT 100]));
                // sets the number of records in each page set
                leadCon.setPageSize(10);
            }
            return leadCon;
        }
        set;
    }
    // returns a list of leads  for the sObjects in the current page set
    Public List<Lead> getLoadData(){
        leadList = new List<Lead>();
        for (lead singleLead:(List<Lead>)leadCon.getRecords()){
            leadList.add(singleLead);
         }
         return leadList;
    }
   
    //New Lead Button
    Public pagereference newLead(){
        PageReference pageref = new PageReference('/00Q/e?retURL=%2F00Q%2Fo&RecordType={!RecordtypeIdlead}&ent=Lead');
        return pageref;
    }
    //Delete method on click of delete link on the page
    public pagereference deleteLead()
   {
      // if for any reason we are missing the reference 
      if (selectedLeadId == null) {
         return null;
      }
     
      // find the account record within the collection
      Lead toBeDeleted = null;
      for(Lead singleLead : leadList){
       if (singleLead.Id == SelectedLeadId) {
          toBeDeleted = singleLead;
          break;
       }
      }
      //if account record found delete it
      if (toBeDeleted != null) {
       try{Delete toBeDeleted;}
       catch(DMLException e){
           System.debug('NMRHotLeadcontroller: Error deleting the lead. ' + e.getMessage());
       }
      }
     
      //refresh the data
      pagereference pageRef = new pagereference('/apex/NMRHotLeadPage');
      pageRef.setredirect(true);
      return pageRef;
   }    
   // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            return leadcon.getHasNext();
        }
        set;
    }
 
    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return leadcon.getHasPrevious();
        }
        set;
    }
    // returns the previous page of records
    public void previous() {
        leadcon.previous();
    }
 
    // returns the next page of records
    public void next() {
        leadcon.next();
    }
    //Test Method
    @isTest(SeeAllData=true)
    static void myTest() {
          List<Lead> Leads= new List<Lead>{};
    test.startTest();    
    for(Integer i = 0; i < 100; i++){
        Lead l = new Lead(lastName = 'Test Lead' + i,recordtypeid=RecordTypeHelper.GetRecordTypeID('Lead', 'NMR Lead'),phone='999299564'+i,NumberOfEmployees = 100,Company='testcomp'+i,Status='Closed',Flow_Progress__c='EC- Purchase Completion',Lead_Type__c='Inquiry+Listing',LeadSource ='AdStore',Last_Enrichment_Date__c=System.Now());
        Leads.add(l);
    }
   
    
         insert Leads;
         system.debug(Leads);
        PageReference pageRef = Page.NMRHotLeadPage;
         Test.setCurrentPage(pageRef);
         ApexPages.StandardSetController ctrl = new ApexPages.StandardSetController(leads);
         NMRHotLeadcontroller controller = new NMRHotLeadcontroller(ctrl);
         controller.getLoadData();
         controller.newLead();
         controller.deleteLead();
         controller.SelectedLeadId = leads[1].Id;
         controller.deleteLead();
         controller.hasNext = TRUE;
         controller.hasprevious = TRUE;
         controller.Next();
         controller.previous();
    test.stopTest();
       }
}